//
//  CHRepairHeaderView.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/9.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHRepairHeaderView.h"
#import "CHStarView.h"
@interface CHRepairHeaderView()
{
    
    UIImageView *photoView;
    UILabel *titleLabel;
    CHStarView *starView;
    UILabel *addressLabel;
    UILabel *levelsLabel;
    UILabel *nameLabel;
    UILabel *phoneNumLabel;
    UILabel *usernameLabel;
    UILabel *pwdLabel;
    UIButton *coverBtn;
    UIImageView *imgView;
    NSString *imgName;
    UIImageView *nameIcon;
    UIImageView *phoneIcon;
    UIView *lineView1;
    UIView *lineView2;
}

@end

@implementation CHRepairHeaderView

+(instancetype)headView{
    
  return [[self alloc] init];
    
}


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
      
        
        [self setupUI];
    }
    return self;
}


-(void)setupUI{
    
//    企业图片
    photoView = [[UIImageView alloc] init];
    photoView.image = [UIImage imageNamed:@"placeholder"];
    photoView.layer.cornerRadius = 3;
    photoView.layer.masksToBounds = YES;
    photoView.userInteractionEnabled = YES;
    [self addSubview:photoView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [photoView addGestureRecognizer:tap];
    
//    企业名称
    titleLabel = [[UILabel alloc] init];
    titleLabel.font = Font(14);
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.numberOfLines = 0;
    titleLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    [self addSubview:titleLabel];
    
//    评分
    starView = [[CHStarView alloc] init];
//    starView.backgroundColor = [UIColor greenColor];
    starView.starNum = 0;
    [self addSubview:starView];
    
    addressLabel = [[UILabel alloc] init];
    addressLabel.textColor = [UIColor colorWithHexString:@"888888" alpha:1.0f];
    addressLabel.textAlignment = NSTextAlignmentLeft;
    addressLabel.font = Font(12);
    addressLabel.numberOfLines = 0;
    [self addSubview:addressLabel];
    
    
    usernameLabel = [[UILabel alloc] init];
    usernameLabel.textColor =  [UIColor colorWithHexString:@"888888" alpha:1.0f];
    usernameLabel.textAlignment = NSTextAlignmentLeft;
    usernameLabel.font = Font(12);
    [self addSubview:usernameLabel];
    
    
    pwdLabel = [[UILabel alloc] init];
    pwdLabel.textColor =  [UIColor colorWithHexString:@"888888" alpha:1.0f];
    pwdLabel.textAlignment = NSTextAlignmentLeft;
    pwdLabel.font = Font(12);
    [self addSubview:pwdLabel];
    
    levelsLabel = [[UILabel alloc] init];
    levelsLabel.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    levelsLabel.font = Font(12);
    levelsLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:levelsLabel];
    
    nameIcon = [[UIImageView alloc] init];
    nameIcon.image = [UIImage imageNamed:@"name"];
    [self addSubview:nameIcon];
    
    lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView1];
    
    
    nameLabel = [[UILabel alloc] init];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.font = Font(12);
    nameLabel.textColor = [UIColor colorWithHexString:@"888888" alpha:1.0f];
    [self addSubview:nameLabel];
    
    phoneIcon = [[UIImageView alloc] init];
    phoneIcon.image = [UIImage imageNamed:@"phone"];
    [self addSubview:phoneIcon];
    
    lineView2 = [[UIView alloc] init];
    lineView2.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView2];
    
    phoneNumLabel = [[UILabel alloc] init];
    phoneNumLabel.userInteractionEnabled = YES;
    phoneNumLabel.textAlignment = NSTextAlignmentLeft;
    phoneNumLabel.font = Font(12);
    phoneNumLabel.textColor = [UIColor colorWithHexString:@"888888" alpha:1.0f];
    [self addSubview:phoneNumLabel];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callNum)];
    [phoneNumLabel addGestureRecognizer:tap1];
    
    
}

- (void)callNum{
    
    
    NSLog(@"点击了");
    
    // 第三种：通过UIWebView打电话。有提示，打完电话能自动回到应用
    UIWebView *wb = [[UIWebView alloc] init];
    NSString *urlStr = [NSString stringWithFormat:@"tel://%@",phoneNumLabel.text];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [wb loadRequest:request];
    [self addSubview:wb];
    
}



-(void)tapGesture:(UITapGestureRecognizer *)tap{
    
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    // 创建遮罩层
    coverBtn = [[UIButton alloc] init];
    
    //  设置遮罩层的背景颜色
    coverBtn.backgroundColor = [UIColor blackColor];
    //  设置frame
    coverBtn.frame = window.bounds;
    //  添加到控制器view中
    [window addSubview:coverBtn];

    //  设置遮罩层透明度
    coverBtn.alpha = 0.0;
    
    //  让图片按钮显示在控制器view最前面 Front前面
    imgView = [[UIImageView alloc] init];
    [imgView sd_setImageWithURL:[NSURL URLWithString:imgName] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    CGRect imgViewFrm = [photoView.superview convertRect:photoView.frame toView:window];
    
    imgView.frame = imgViewFrm;
    
    [window addSubview:imgView];
   
    //    [window bringSubviewToFront:self.headImgView];
    
    [UIView animateWithDuration:0.5 animations:^{
        imgView.frame = CGRectMake(0, (UIScreenH - UIScreenW)/2, UIScreenW, UIScreenW);
        //        设置遮罩层透明度
        coverBtn.alpha = 0.5;
    }];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallImage)];
    [imgView addGestureRecognizer:tap1];
    
    //  监听遮罩层按钮的点击
    [coverBtn addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)smallImage{
    CGRect imgViewFrm = [photoView.superview convertRect:photoView.frame toView:nil];
    [UIView animateWithDuration:0.5 animations:^{
        //  还原图片按钮
        imgView.frame = imgViewFrm;
        //      设置遮罩层慢慢变透明
        coverBtn.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        //      当动画执行完毕就会调用这个block
        //  移除遮罩层
        [coverBtn removeFromSuperview];
        [imgView removeFromSuperview];
    }];
    
}

-(void)setCompanyid:(NSString *)companyid{
    _companyid = companyid;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = self.areaid;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:companyid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"companyid"] = companyid;
    NSLog(@"%@",params);
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovcompanydetails" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        NSLog(@"%@",result);
        if ([result[@"code"] isEqualToString:@"1"]) {
            
            NSDictionary *dict = result[@"data"];
            imgName = dict[@"companyphoto"];
            [photoView sd_setImageWithURL:[NSURL URLWithString:dict[@"companyphoto"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            addressLabel.text = [NSString stringWithFormat:@"地址:%@",dict[@"address"]];
            nameLabel.text = dict[@"legalname"];
            titleLabel.text = dict[@"name"];
            starView.starNum = [dict[@"evaluatetotle"] intValue];
            phoneNumLabel.text = dict[@"legaltel"];
            usernameLabel.text = [NSString stringWithFormat:@"登录名：%@",dict[@"companylogin"]];
            pwdLabel.text = [NSString stringWithFormat:@"密码：%@",dict[@"companypass"]];
            NSString *str = nil;
            if ([dict[@"levels"] isEqualToString:@"01"]) {
                str = @"一类维修经营业务";
            }else if([dict[@"levels"] isEqualToString:@"02"]){
                str = @"二类维修经营业务";
            }else if([dict[@"levels"] isEqualToString:@"03"]){
                str = @"三类维修经营业务";
            }else{
                str = @"其他";
            }
            levelsLabel.text = str;
        }
    }];
    
    
    
    
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    titleLabel.frame = CGRectMake( 10, 10, UIScreenW - 20, 30 *ScaleY);
    starView.frame = CGRectMake(10, CGRectGetMaxY(titleLabel.frame), 100*ScaleX, 15*ScaleY);
    photoView.frame = CGRectMake(10, CGRectGetMaxY(starView.frame)+10, UIScreenW / 3  , 90 * ScaleY);
    CGFloat photoViewMaxX = CGRectGetMaxX(photoView.frame);
    levelsLabel.frame = CGRectMake(10, CGRectGetMaxY(photoView.frame), photoView.w, self.h - CGRectGetMaxY(photoView.frame));
    addressLabel.frame = CGRectMake(CGRectGetMaxX(photoView.frame)+10, CGRectGetMaxY(starView.frame)+5, UIScreenW - CGRectGetMaxX(photoView.frame) - 20, 35*ScaleY);
    
    usernameLabel.frame = CGRectMake(addressLabel.x, CGRectGetMaxY(addressLabel.frame), addressLabel.w, 30);
    
    pwdLabel.frame = CGRectMake(addressLabel.x, CGRectGetMaxY(usernameLabel.frame), addressLabel.w, 30);
    
    nameIcon.frame = CGRectMake(photoViewMaxX+10, CGRectGetMaxY(photoView.frame) + (levelsLabel.h - 10)/2, 10, 10);
    lineView1.frame = CGRectMake(CGRectGetMaxX(nameIcon.frame) +5, nameIcon.y, 1, 10);
    nameLabel.frame = CGRectMake(CGRectGetMaxX(lineView1.frame) +5, levelsLabel.y, 60*ScaleX, levelsLabel.h);
    phoneIcon.frame = CGRectMake(CGRectGetMaxX(nameLabel.frame)+5*ScaleX, nameIcon.y, 10, 10);
    lineView2.frame = CGRectMake(CGRectGetMaxX(phoneIcon.frame)+5, phoneIcon.y, 1, 10);
    phoneNumLabel.frame = CGRectMake(CGRectGetMaxX(lineView2.frame)+5, nameLabel.y, UIScreenW - CGRectGetMaxX(lineView2.frame)-5, levelsLabel.h);
    
}


@end
